package impl

import (
	"adam2/internal/constants"
	"adam2/internal/dto"
	"adam2/internal/vo"
	"strings"
	"time"
)

type RelStockInfoConceptBoardDaoImpl struct {
	*BaseDaoImpl
}

// 返回dao实现类
func GetRelStockInfoConceptBoardDaoImpl() *RelStockInfoConceptBoardDaoImpl {
	return &RelStockInfoConceptBoardDaoImpl{GetBaseDaoImpl()}
}

// 返回表名
func (_relStockInfoConceptBoardDaoImpl *RelStockInfoConceptBoardDaoImpl) FindTableName() string {
	return "REL_STOCK_INFO_CONCEPT_BOARD"
}

// 分页显示
func (_relStockInfoConceptBoardDaoImpl *RelStockInfoConceptBoardDaoImpl) Page(orderByColumnArray []vo.OrderByColumn, conceptBoardName string, transactionDate time.Time, pageNo int, pageSize int) dto.RelStockInfoConceptBoardDtoArray {
	var relStockInfoConceptBoardDtoArray dto.RelStockInfoConceptBoardDtoArray
	var offset int = (pageNo - 1) * pageSize
	db := _relStockInfoConceptBoardDaoImpl.db.Distinct().Table("CONCEPT_BOARD t")
	db.Select("stda.code_ as CODE, si.name_ as NAME, t.myquant_code as MYQUANT_CODE, T.akshare_code as AKSHARE_CODE, " +
		"stda.close_price as CLOSE_PRICE, " +
		"mtsd.UP_DOWN_PERCENTAGE_2 as UP_DOWN_PERCENTAGE_2, mtsd.UP_DOWN_PERCENTAGE_3 as UP_DOWN_PERCENTAGE_3, " +
		"mtsd.UP_DOWN_PERCENTAGE_4 as UP_DOWN_PERCENTAGE_4, mtsd.UP_DOWN_PERCENTAGE_5 as UP_DOWN_PERCENTAGE_5, " +
		"mtsd.UP_DOWN_PERCENTAGE_10 as UP_DOWN_PERCENTAGE_10, " +
		"mtsd.UP_DOWN_PERCENTAGE_20 as UP_DOWN_PERCENTAGE_20")
	db.Joins("join REL_STOCK_INFO_CONCEPT_BOARD rsicb on rsicb.CONCEPT_BOARD_ID=t.ID " +
		"join stock_info si on rsicb.STOCK_INFO_ID=si.id_ " +
		"join STOCK_TRANSACTION_DATA_ALL stda on stda.code_=si.code_ " +
		"join MDL_TOP_STOCK_DETAIL mtsd on mtsd.code_=si.code_")
	if nil != orderByColumnArray && len(orderByColumnArray) > 0 {
		var orderBy strings.Builder
		for _, orderByColumn := range orderByColumnArray {
			orderBy.WriteString(orderByColumn.ColumnName)
			orderBy.WriteString(" ")
			if orderByColumn.AscOrDesc == constants.ASC {
				orderBy.WriteString(constants.ASC)
			}
			if orderByColumn.AscOrDesc == constants.DESC {
				orderBy.WriteString(constants.DESC)
			}
			orderBy.WriteString(",")
		}
		var order string = orderBy.String()[0 : len(orderBy.String())-1]
		db = db.Order(order)
	}
	db = db.Offset(offset).Limit(pageSize)
	if !transactionDate.IsZero() && conceptBoardName != "" {
		db = db.Where("mtsd.date_=?", transactionDate).
			Where("stda.date_=?", transactionDate).
			Where("t.name=?", conceptBoardName)
	}
	db.Find(&relStockInfoConceptBoardDtoArray)
	return relStockInfoConceptBoardDtoArray
}

// 分页显示，返回总数
func (_relStockInfoConceptBoardDaoImpl *RelStockInfoConceptBoardDaoImpl) PageTotal(conceptBoardName string, transactionDate time.Time) int {
	var relStockInfoConceptBoardDtoArray dto.RelStockInfoConceptBoardDtoArray
	db := _relStockInfoConceptBoardDaoImpl.db.Table("CONCEPT_BOARD t")
	db.Distinct().Select("si.name_ as NAME, t.myquant_code as MYQUANT_CODE, t.akshare_code as AKSHARE_CODE, " +
		"stda.close_price as CLOSE_PRICE, stda.last_close_price as LAST_CLOSE_PRICE, " +
		"mtsd.UP_DOWN_PERCENTAGE_2 as UP_DOWN_PERCENTAGE_2, mtsd.UP_DOWN_PERCENTAGE_3 as UP_DOWN_PERCENTAGE_3, " +
		"mtsd.UP_DOWN_PERCENTAGE_4 as UP_DOWN_PERCENTAGE_4," +
		"mtsd.UP_DOWN_PERCENTAGE_5 as UP_DOWN_PERCENTAGE_5, mtsd.UP_DOWN_PERCENTAGE_10 as UP_DOWN_PERCENTAGE_10, " +
		"mtsd.UP_DOWN_PERCENTAGE_20 as UP_DOWN_PERCENTAGE_20")
	db.Joins("join REL_STOCK_INFO_CONCEPT_BOARD rsicb on rsicb.CONCEPT_BOARD_ID=t.ID " +
		"join stock_info si on rsicb.STOCK_INFO_ID=si.id_ " +
		"join STOCK_TRANSACTION_DATA_ALL stda on stda.code_=si.code_ " +
		"join MDL_TOP_STOCK_DETAIL mtsd on mtsd.code_=si.code_")
	if !transactionDate.IsZero() && conceptBoardName != "" {
		db = db.Where("mtsd.date_=?", transactionDate).
			Where("stda.date_=?", transactionDate).
			Where("t.name=?", conceptBoardName)
	}
	db.Find(&relStockInfoConceptBoardDtoArray)
	return len(relStockInfoConceptBoardDtoArray)
}
